﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using br.com.SistemaChamados.Util;
using br.com.SistemaChamados.QueryManager.ReaderParser;
using System.Reflection;

namespace br.com.SistemaChamados.DAO.QueryManager.ReaderParser
{
    public class DataParser
    {

        private Type _aType;
        private IDataReader _aReader;

        public DataParser()
        {
            //Empty Constructor
        }

        public DataParser(Type _type, IDataReader _reader)
        {
            this._aType = _type;
            this._aReader = _reader;
        }

        public List<object> processAndConvertIDataReader()
        {
            List<object> resultList = new List<object>();
            Type[] arrayTypes = getFieldTypes();
            while (_aReader.Read())
            {
                Object obj = ReflectionUtils.createAnObjectFromType(_aType);
                for (int i = 0; i < arrayTypes.Length; i++)
                {
                    Type fieldType = arrayTypes[i];
                    //PropertyInfo propertyInfo = ReflectionUtils.getPropertyByName(_aType, "insert a name");
                    //propertyInfo.SetValue(obj, DataBinder., null);
                }
            }
            return resultList;
        }

        private Type[] getFieldTypes()
        {
            Type[] fieldTypes = new Type[_aReader.FieldCount];
            for (int i = 0; i < _aReader.FieldCount; i++)
            {
                fieldTypes[i] = _aReader.GetFieldType(i);
            }
            return fieldTypes;
        }

        private string[] getFieldNames()
        {
            string[] fieldNames = new string[_aReader.FieldCount];
            for (int i = 0; i < _aReader.FieldCount; i++)
            {
                fieldNames[i] = _aReader.GetSchemaTable().Columns[i].ColumnName;
            }
            return fieldNames;
        }
    }
}